﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Xml;
using Ccit.Utils;
using System.Web.SessionState;
using Ccit.OA.Utils;
using System.Collections;
using System.Collections.Generic;
using Ccit.Global;
using System.Text;
using Ccit.OA.MsSystemDoc;

namespace Ccit.OA.SystemDoc
{
    public class SystemDocSQL : ISystemDoc
    {
        #region 获取类别树根节点名称
        /// <summary>
        /// 获取类别树根节点名称
        /// </summary>
        /// <param name="sClassType"></param>
        /// <returns></returns>
        public string GetSysDocNode(string sClassType, string swhereClause)
        {
            string iNum = "doc_b1_20";
            if (swhereClause == "1")
            {
                iNum = "doc_b1_10";
            }
            string sQuery = "";
            if (sClassType != "")
                sQuery = " and doc_b1_10='" + sClassType + "'";
            string cmdText = "select " + iNum + " as NodeName from doc_b1 where 1=1 " + sQuery + "";
            string NodeName = "";
            //DataSet dSet = DataAccessor.ExecuteDataSet(cmdText);
            //if (dSet.Tables[0].Rows.Count > 0)
            //{
            //    NodeName = dSet.Tables[0].Rows[0][0].ToString();
            //}
            using (IDataReader iReader = DataAccessor.ExecuteReader(cmdText))
            {
                if (iReader.Read())
                {
                    NodeName = iReader["NodeName"].ToString();
                }
            }
            return NodeName;
        }

        #endregion

        #region 获取菜单树信息

        /// <summary>
        /// 获取菜单树信息

        /// </summary>
        /// <returns></returns>
        public string GetSysDocNodes(string sClassType, string swhereClause)
        {
            string iNum = "DOC_B2_30";
            if (swhereClause == "1")
            {
                iNum = "DOC_B2_20";
            }
            string sQuery = "";
            if (sClassType != "")
                sQuery = " and DOC_B2_90='" + sClassType + "'";
            string cmdText = "SELECT DOC_B2_10 AS SysDocID,DOC_B2_20 AS LAYCODE," + iNum + " AS SysDocNAME FROM DOC_B2 " +
                           " left join DOC_B1 on DOC_B1_10 = DOC_B2_90 " +
                          "WHERE DOC_B2_40=0 AND DOC_B2_100='0' " + sQuery + " ORDER BY DOC_B2_20 ASC";
            StringBuilder sbXML = new StringBuilder("<ADDRS>");
            string SysDocID = "";
            string addrLayCode = "";
            string SysDocNAME = "";
            string hasChild = "0";
            using (IDataReader iReader = DataAccessor.ExecuteReader(cmdText))
            {
                while (iReader.Read())
                {
                    SysDocID = iReader["SysDocID"].ToString();
                    addrLayCode = iReader["LAYCODE"].ToString();
                    SysDocNAME = iReader["SysDocNAME"].ToString();
                    hasChild = (isChildAddr(SysDocID)) ? "1" : "0";
                    sbXML.Append("<ADDR HASCHILD=\"").Append(hasChild).Append("\">");
                    sbXML.Append("<SysDocID>").Append(SysDocID).Append("</SysDocID>");
                    sbXML.Append("<SysDocNAME><![CDATA[").Append(SysDocNAME).Append("]]></SysDocNAME>");
                    sbXML.Append("</ADDR>");
                }
            }

            sbXML.Append("</ADDRS>");
            return sbXML.ToString();
        }
        private bool isChildAddr(string addrLayCode)
        {
            bool bHas = false;
            if (string.IsNullOrEmpty(addrLayCode)) return bHas;

            int layCodeLen = addrLayCode.Length;
            string cmdText = "SELECT COUNT(DOC_B2_10) AS CHILDCOUNT FROM DOC_B2 WHERE DOC_B2_40 = '" + addrLayCode + "' " +
                             " AND DOC_B2_100='0'";
            string childCount = "";
            using (IDataReader iReader = DataAccessor.ExecuteReader(cmdText))
            {
                if (iReader.Read() && null != iReader[0])
                {
                    childCount = iReader["CHILDCOUNT"].ToString();
                    if (!string.IsNullOrEmpty(childCount) && childCount != "0")
                        bHas = true;
                }
            }

            return bHas;
        }
        private bool isHasChildAddr(string addrLayCode)
        {
            bool bHas = false;
            if (string.IsNullOrEmpty(addrLayCode)) return bHas;

            int layCodeLen = addrLayCode.Length;
            string cmdText = "SELECT COUNT(DOC_B2_10) AS CHILDCOUNT FROM DOC_B2 WHERE DOC_B2_20 LIKE '" + addrLayCode + "%' " +
                             "AND LEN(DOC_B2_20)>" + layCodeLen + " AND DOC_B2_100='0'";
            string childCount = "";
            using (IDataReader iReader = DataAccessor.ExecuteReader(cmdText))
            {
                if (iReader.Read() && null != iReader[0])
                {
                    childCount = iReader["CHILDCOUNT"].ToString();
                    if (!string.IsNullOrEmpty(childCount) && childCount != "0")
                        bHas = true;
                }
            }

            return bHas;
        }
        public string GetNodeId(string systemId)
        {
            string cmdText = "select  DOC_B2_40 from DOC_B2 where DOC_B2_10=" + systemId;
            string iNodeId = "";
            using (IDataReader iReader = DataAccessor.ExecuteReader(cmdText))
            {
                if (iReader.Read())
                { iNodeId = iReader["DOC_B2_40"].ToString(); }
            }
            return iNodeId;
        }
        #endregion

        #region 获取菜单树子节点信息
        /// <summary>
        /// 获取菜单树子节点信息
        /// </summary>
        /// <param name="SysDocId"></param>
        /// <returns></returns>
        public List<MsSecondClass> GetSysDocNodesXML(string SysDocId, string sClassType, string swhereClause)
        {
            List<MsSecondClass> lstSec = null;
            string iNum = "DOC_B2_30";
            if (swhereClause == "1")
            {
                iNum = "DOC_B2_20";
            }
            string sQuery = "";
            if (sClassType != "")
                sQuery = " and DOC_B2_90='" + sClassType + "'";
            if (string.IsNullOrEmpty(SysDocId)) return null;

            string upAddrLayCode = getAddrLayCodeById(SysDocId);
            int layCodeLen = upAddrLayCode.Length;
            int nextLayCodeLen = layCodeLen + 4;
            string cmdText = "SELECT DOC_B2_10 AS SysDocID,DOC_B2_20 AS LAYCODE," + iNum + " AS SysDocNAME  FROM DOC_B2 " +
                              " left join DOC_B1 on DOC_B1_10 = DOC_B2_90 " +
                             "WHERE DOC_B2_40= " + SysDocId + " " +
                             " and DOC_B2_100='0' " + sQuery + " ORDER BY DOC_B2_20 ASC";
            StringBuilder sbXML = new StringBuilder("<ADDRS>");
            string SysDocID = "";
            string addrLayCode = "";
            string SysDocNAME = "";
            string hasChild = "0";
            using (IDataReader iReader = DataAccessor.ExecuteReader(cmdText))
            {
                lstSec = new List<MsSecondClass>();
                while (iReader.Read())
                {
                    MsSecondClass ms = new MsSecondClass();
                    SysDocID = iReader["SysDocID"].ToString();
                    addrLayCode = iReader["LAYCODE"].ToString();
                    SysDocNAME = iReader["SysDocNAME"].ToString();
                    hasChild = (isChildAddr(SysDocID)) ? "1" : "0";
                    ms.Sysdocid = SysDocID;
                    ms.Systypename = SysDocNAME;
                    ms.State = hasChild;
                    ms.Sysnumber = "00";
                    lstSec.Add(ms);
                    //sbXML.Append("<ADDR HASCHILD=\"").Append(hasChild).Append("\">");
                    //sbXML.Append("<SysDocID>").Append(SysDocID).Append("</SysDocID>");
                    //sbXML.Append("<SysDocNAME><![CDATA[").Append(SysDocNAME).Append("]]></SysDocNAME>");
                    //sbXML.Append("</ADDR>");                  
                }
            }
            return lstSec;
            //sbXML.Append("</ADDRS>");
            //return sbXML.ToString();
        }

        public List<MsSecondClass> GetSysDocNodesXMLmanage(string FolderId, string sClassType, string sNodeTab)
        {
            string sShare;
            List<MsSecondClass> lstSec = null;
            string iNum = "DOC_B2_30";
            if (sNodeTab == "1")
            {
                iNum = "DOC_B2_20";
            }
            string sQuery = "";
            if (sClassType != "")
                sQuery = " and DOC_B2_90='" + sClassType + "'";
            if (string.IsNullOrEmpty(FolderId)) return null;
            //sShare = GetShare(FolderId);
            #region 当前用户对该文件夹以及文件夹的子目录无任何操作权限时
            //if (sShare == "05")
            //{
            //    return null;
            //}
            //else {
            string cmdText = "SELECT DOC_B2_10 AS SysDocID,DOC_B2_20 AS LAYCODE," + iNum + " AS SysDocNAME  FROM DOC_B2 " +
                                  " WHERE  DOC_B2_40 =" + FolderId +
                                 " and DOC_B2_100='0' " + sQuery + " ORDER BY DOC_B2_20 ASC";
            StringBuilder sbXML = new StringBuilder("<ADDRS>");
            string SysDocID = "";
            string addrLayCode = "";
            string SysDocNAME = "";
            string hasChild = "0";
            using (IDataReader iReader = DataAccessor.ExecuteReader(cmdText))
            {
                lstSec = new List<MsSecondClass>();
                while (iReader.Read())
                {
                    MsSecondClass ms = new MsSecondClass();
                    SysDocID = iReader["SysDocID"].ToString();
                    addrLayCode = iReader["LAYCODE"].ToString();
                    SysDocNAME = iReader["SysDocNAME"].ToString();
                    hasChild = (isChildAddr(SysDocID)) ? "1" : "0";
                    ms.Sysdocid = SysDocID;
                    ms.Systypename = SysDocNAME;
                    ms.State = hasChild;
                    ms.Sysnumber = GetShare(SysDocID);
                    lstSec.Add(ms);
                }
                //}
                return lstSec;
            }
            #endregion

        }
        public string getAddrLayCodeById(string filterAddrId)
        {
            string addrLayCode = "";
            if (string.IsNullOrEmpty(filterAddrId)) return addrLayCode;

            string cmdText = "SELECT DOC_B2_20 AS LAYCODE FROM DOC_B2  WHERE DOC_B2_10='" + filterAddrId + "'";
            using (IDataReader iReader = DataAccessor.ExecuteReader(cmdText))
            {
                if (iReader.Read() && null != iReader[0])
                {
                    addrLayCode = iReader["LAYCODE"].ToString();
                }
            }

            return addrLayCode;
        }
        private string getAddrLayCodeById(string filterAddrId, string Query)
        {
            string addrLayCode = "";
            if (string.IsNullOrEmpty(filterAddrId)) return addrLayCode;

            string cmdText = "SELECT DOC_B2_20 AS LAYCODE FROM DOC_B2  WHERE DOC_B2_40='" + filterAddrId + "'";
            using (IDataReader iReader = DataAccessor.ExecuteReader(cmdText))
            {
                if (iReader.Read() && null != iReader[0])
                {
                    addrLayCode = iReader["LAYCODE"].ToString();
                }
            }

            return addrLayCode;
        }
        #endregion

        #region 删除节点信息
        /// <summary>
        /// 删除节点信息
        /// </summary>
        /// <param name="SysDocId"></param>
        /// <returns></returns>
        public List<string> DeleteSysDocMess(string SysDocId)
        {
            List<string> lstResult = new List<string>();
            if (string.IsNullOrEmpty(SysDocId))
            {
                lstResult.Add("-999");
                lstResult.Add("操作失败:缺少必要参数!");
            }
            string cmdText = "UPDATE DOC_B2 SET DOC_B2_100='1' WHERE DOC_B2_10='" + SysDocId + "'";
            int iexe = DataAccessor.ExecuteNonQuery(cmdText);
            if (iexe > 0)
            {
                lstResult.Add(SysDocId);
                lstResult.Add("删除成功!");
            }
            else
            {
                lstResult.Add("-1");
                lstResult.Add("操作失败:服务器忙,请稍后再试!");
            }

            return lstResult;
        }

        #endregion

        #region 获取编辑页面的上级栏目信息

        /// <summary>
        /// 获取编辑页面的上级栏目信息

        /// </summary>
        /// <param name="filterSysDocId"></param>
        /// <param name="sClassType"></param>
        /// <returns></returns>
        public List<DictLib> GetAllSysDocMess(string filterSysDocId, string sClassType)
        {
            List<DictLib> lstDict = new List<DictLib>();
            string filterAddrLayCode = "";
            if (!string.IsNullOrEmpty(filterSysDocId))
                filterAddrLayCode = getAddrLayCodeById(filterSysDocId, "");
            string sQuery = "";
            if (sClassType != "")
                sQuery = " and DOC_B2_90='" + sClassType + "'";
            string whereClause = " 1=1 AND DOC_B2_100='0' " + sQuery + " ";
            if (!string.IsNullOrEmpty(filterAddrLayCode))
                whereClause += "AND DOC_B2_10 NOT LIKE '" + filterAddrLayCode + "%'";
            string cmdText = "SELECT DOC_B2_10 AS SysDocID,DOC_B2_20 AS LAYCODE,DOC_B2_30 AS SysDocNAME FROM DOC_B2 " +
                             " left join DOC_B1 on DOC_B1_10 = DOC_B2_90  WHERE " + whereClause + " ORDER BY DOC_B2_20 ASC";
            string SysDocID = "";
            string addrLayCode = "";
            string SysDocNAME = "";
            DictLib dict;
            using (IDataReader iReader = DataAccessor.ExecuteReader(cmdText))
            {
                while (iReader.Read())
                {
                    SysDocID = iReader["SysDocID"].ToString();
                    addrLayCode = iReader["LAYCODE"].ToString();
                    SysDocNAME = iReader["SysDocNAME"].ToString();
                    SysDocNAME = dealAddrName(addrLayCode, SysDocNAME);
                    dict = new DictLib(SysDocNAME, SysDocID);
                    lstDict.Add(dict);
                }
            }

            return lstDict;
        }
        private string dealAddrName(string addrLayCode, string SysDocNAME)
        {
            string retName = "";
            if (string.IsNullOrEmpty(SysDocNAME)) return "";

            int layCodeLen = addrLayCode.Length;
            int iQuot = layCodeLen / 4;
            if (addrLayCode.Length > 4)
            {
                if (addrLayCode.Length < 8)
                { iQuot = 2; }
                if (addrLayCode.Length > 8 && addrLayCode.Length < 11)
                { iQuot = 3; }
            }
            int circleCount = iQuot - 1;
            for (int i = 1; i <= circleCount; i++)
                retName += "\u3000";

            retName += SysDocNAME;
            return retName;
        }
        #endregion

        #region 保存系统类别信息
        /// <summary>
        /// 保存系统类别信息
        /// </summary>
        /// <param name="ms"></param>
        /// <returns></returns>
        public List<string> DoSaveSysDocMess(MsSecondClass ms)
        {
            List<string> lstBack = new List<string>();
            List<string> lstResult = new List<string>();
            List<string> lstCount = new List<string>();
            LoginUser loginUser = (LoginUser)HttpContext.Current.Session["LogUserInfo"];
            string cmdText = "";
            string isAddNew = "";
            string Sysdocid = "";
            if (ms.Manageupaddr != "")
            {
                if (ms.Superiorid != "")
                {
                    lstCount = GetSysDocIdInfo(ms.Superiorid, ms.Primary);
                    if (lstCount.Count > 0)
                    {
                        ms.Manageid = lstCount[0];
                        ms.Managemark = lstCount[1];
                    }
                }
            }
            if (ms.Viewupaddr != "")
            {
                if (ms.Superiorid != "")
                { 
                    if (lstCount.Count > 0)
                    {
                        ms.Viewid = lstCount[2];
                        ms.Viewmark = lstCount[3];
                    }
                }
            }
            if (ms.Superiorid == "")
                ms.Superiorid = "0";
            string fileUrl = "";
            if (ms.Superiorid != "")
            {
                GetFileUrl(ref fileUrl, ms.Superiorid);
            }
            if (ms.Sysdocid == "")
            {
                string curYear = DateTime.Now.ToString("yyyy-MM-dd");
                ms.Sysdocid = DataInterface.GetMaxId("DOC_B2", "DOC_B2_10").ToString();
                cmdText = "insert into DOC_B2(DOC_B2_10,DOC_B2_20,DOC_B2_30,DOC_B2_40,DOC_B2_50,DOC_B2_60,DOC_B2_70," +
                        "DOC_B2_80,DOC_B2_90,DOC_B2_100,DOC_B2_110,DOC_B2_9990,DOC_B2_9991,DOC_B2_9992,DOC_B2_9993,DOC_B2_9994,DOC_B2_9995)" +
                        " values(" + ms.Sysdocid + ",'" + ms.Sysnumber + "','" + ms.Systypename.Replace("'", "''") + "'," +
                        "" + ms.Superiorid + ",'" + ms.Manageid + "','" + ms.Managemark + "','" + ms.Viewid + "','" + ms.Viewmark + "','" + ms.Primary + "',0,'" + fileUrl + "'," +
                        "'" + loginUser.OrganId + "','" + loginUser.DeptId + "','" + loginUser.PosId + "','" + loginUser.ClassId + "','" + loginUser.UserId + "','" + curYear + "')";
                lstResult.Add(cmdText);
                isAddNew = "0";
            }
            else
            {
                cmdText = "update DOC_B2 set DOC_B2_20='" + ms.Sysnumber + "',DOC_B2_30='" + ms.Systypename.Replace("'", "''") + "'," +
                        "DOC_B2_40=" + ms.Superiorid + ",DOC_B2_50='" + ms.Manageid + "',DOC_B2_60='" + ms.Managemark + "'," +
                        "DOC_B2_70='" + ms.Viewid + "',DOC_B2_80='" + ms.Viewmark + "',DOC_B2_110='" + fileUrl + "'  WHERE DOC_B2_10=" + ms.Sysdocid;
                lstResult.Add(cmdText);
                isAddNew = "1";
            }
            if (ms.Managelower != "")
            {
                Sysdocid = GetSysDocId(ms.Sysdocid, ms.Primary);
                if (Sysdocid != "")
                {
                    cmdText = "update DOC_B2 set DOC_B2_50='" + ms.Manageid + "',DOC_B2_60='" + ms.Managemark + "' where DOC_B2_10 in(" + Sysdocid + ")";
                    lstResult.Add(cmdText);
                }
            }
            if (ms.Viewlower != "")
            {
                Sysdocid = GetSysDocId(ms.Sysdocid, ms.Primary);
                if (Sysdocid != "")
                {
                    cmdText = "update DOC_B2 set DOC_B2_70='" + ms.Viewid + "',DOC_B2_80='" + ms.Viewmark + "' where DOC_B2_10 in(" + Sysdocid + ")";
                    lstResult.Add(cmdText);
                }
            }
            int iNum = DataAccessor.ExecuteNonQuery(lstResult);
            if (iNum > 0)
            {
                lstBack.Add(ms.Sysdocid);
                lstBack.Add("操作成功!");
                lstBack.Add(ms.Systypename);
                lstBack.Add("1");
                lstBack.Add(isAddNew);
            }
            else
            {
                lstBack.Add("-999");
                lstBack.Add("服务器忙,请稍候重新操作!");
                lstBack.Add("");
                lstBack.Add("0");
                lstBack.Add(isAddNew);
            }
            return lstBack;
        }
        private void GetFileUrl(ref string fileUrl, string Superiorid)
        {
            string cmdText = "select DOC_B2_10,DOC_B2_40 from DOC_B2 where DOC_B2_10=" + Superiorid;
            using (IDataReader iReader = DataAccessor.ExecuteReader(cmdText))
            {
                if (iReader.Read())
                {
                    fileUrl += (fileUrl == "") ? iReader["DOC_B2_10"].ToString() : "," + iReader["DOC_B2_10"].ToString();

                    if (iReader["DOC_B2_40"].ToString() != "0")
                    {
                        GetFileUrl(ref fileUrl, iReader["DOC_B2_40"].ToString());
                    }
                }
            }

        }
        public string GetSysDocId(string sNumber, string sClassType)
        {
            int layCodeLen = sNumber.Length;
            string cmdText = "SELECT DOC_B2_10 AS SysDocID  FROM DOC_B2 ";
            cmdText += " left join DOC_B1 on DOC_B1_10 = DOC_B2_90  ";
            cmdText += " WHERE DOC_B2_40 ="+sNumber+" ";
            cmdText += " and DOC_B2_100='0' and DOC_B2_90='" + sClassType + "'";
            string iSysDocId = "";
            using (IDataReader iReader = DataAccessor.ExecuteReader(cmdText))
            {
                while (iReader.Read())
                {
                    iSysDocId += (iSysDocId == "") ? iReader["SysDocID"].ToString() : "," + iReader["SysDocID"].ToString();
                }
            }
            return iSysDocId;
        }
        private List<string> GetSysDocIdInfo(string superiorid, string sClassType)
        {
            List<string> lstResult = new List<string>();
            string sQuery = "";
            if (sClassType != "")
                sQuery = " and DOC_B2_90='" + sClassType + "'";
            string cmdText = "SELECT DOC_B2_50,DOC_B2_60,DOC_B2_70,DOC_B2_80  FROM DOC_B2 ";
            cmdText += " left join DOC_B1 on DOC_B1_10 = DOC_B2_90  ";
            cmdText += " WHERE DOC_B2_10 ='" + superiorid + "'";
            cmdText += " and DOC_B2_100='0' " + sQuery + "";

            using (IDataReader iReader = DataAccessor.ExecuteReader(cmdText))
            {
                if (iReader.Read())
                {

                    lstResult.Add(iReader["DOC_B2_50"].ToString());
                    lstResult.Add(iReader["DOC_B2_60"].ToString());
                    lstResult.Add(iReader["DOC_B2_70"].ToString());
                    lstResult.Add(iReader["DOC_B2_80"].ToString());
                }
            }
            return lstResult;
        }
        #endregion

        #region 保存成功之后添加树节点信息

        /// <summary>
        /// 保存成功之后添加树节点信息

        /// </summary>
        /// <param name="NodeId"></param>
        /// <returns></returns>
        public string GetNodeLevelStruct(string NodeId)
        {
            string sReturnStruct = string.Empty;
            if (string.IsNullOrEmpty(NodeId)) return sReturnStruct;

            string columnLevelCode = GetMsColumnObj(NodeId);
            string curNodeCode = (null != columnLevelCode && !string.IsNullOrEmpty(columnLevelCode)) ?
                columnLevelCode : "";

            int iCodeLen = curNodeCode.Length;
            int iLev = iCodeLen / 4;
            StringBuilder sbClause = new StringBuilder(" 1=1 ");

            string sClause = "";
            string whereClause = "";
            for (int i = iLev - 1; i >= 0; i--)
            {
                sClause = "SUBSTRING('" + curNodeCode + "',1," + (iCodeLen - i * 4).ToString() + ")";
                whereClause += (string.IsNullOrEmpty(whereClause)) ? sClause : "," + sClause;
            }

            if (!string.IsNullOrEmpty(whereClause))
            {
                sbClause.Append("AND DOC_B2_20 IN (").Append(whereClause).Append(")");
            }
            else
            {
                sbClause.Append("AND 1>1");
            }

            string cmdText = "SELECT DOC_B2_10 AS COLID FROM  DOC_B2 WHERE" + sbClause.ToString() +
                             " ORDER BY DOC_B2_20 ASC";
            using (IDataReader iReader = DataAccessor.ExecuteReader(cmdText))
            {
                while (iReader.Read())
                {
                    sReturnStruct += (string.IsNullOrEmpty(sReturnStruct)) ? iReader["COLID"].ToString() :
                                "," + iReader["COLID"].ToString();
                }
            }


            sbClause.Remove(0, sbClause.ToString().Length);
            sbClause = null;

            return sReturnStruct;
        }
        private string GetMsColumnObj(string dtNodeId)
        {
            string cmdText = "select DOC_B2_20 from DOC_B2 where DOC_B2_10=" + dtNodeId;
            string curNodeCode = "";
            using (IDataReader iReader = DataAccessor.ExecuteReader(cmdText))
            {
                if (iReader.Read())
                {
                    curNodeCode = iReader["DOC_B2_20"].ToString();
                }
            }
            return curNodeCode;
        }
        #endregion

        #region 获取编辑页面信息
        /// <summary>
        /// 获取编辑页面信息
        /// </summary>
        /// <param name="SysDocId"></param>
        /// <param name="sClassType"></param>
        /// <returns></returns>
        public MsSecondClass GetSecondClassMess(string SysDocId, string sClassType)
        {
            string sQuery = "";
            if (sClassType != "")
                sQuery = " and DOC_B2_90='" + sClassType + "'";
            string cmdText = "SELECT DOC_B2_10 AS SysDocID,DOC_B2_20 AS LAYCODE,DOC_B2_30 AS SysDocNAME, " +
                            "DOC_B2_40,DOC_B2_50,DOC_B2_60,DOC_B2_70,DOC_B2_80 " +
                           " FROM DOC_B2  left join DOC_B1 on DOC_B1_10 = DOC_B2_90 " +
                          " WHERE DOC_B2_10=" + SysDocId + " AND DOC_B2_100='0' " + sQuery + "";
            MsSecondClass ms = null;
            using (IDataReader iReader = DataAccessor.ExecuteReader(cmdText))
            {
                if (iReader.Read())
                {
                    ms = new MsSecondClass(iReader["SysDocID"].ToString(), iReader["LAYCODE"].ToString(), iReader["SysDocNAME"].ToString(),
                                          iReader["DOC_B2_40"].ToString(), iReader["DOC_B2_60"].ToString(), iReader["DOC_B2_50"].ToString(),
                                          iReader["DOC_B2_80"].ToString(), iReader["DOC_B2_70"].ToString(), null, null);
                }
            }
            return ms;
        }

        #endregion

        #region 获取编号规则页面树节点名称

        /// <summary>
        /// 获取编号规则页面树节点名称

        /// </summary>
        /// <param name="sClassType"></param>
        /// <returns></returns>
        public List<MsCodeRule> GetCodeRuleNodes(string sClassType)
        {
            string sQuery = "";
            if (sClassType != "")
                sQuery = "and DOC_B4_30='" + sClassType + "'";
            string cmdText = "select  DOC_B4_10,DOC_B4_20 ,DOC_B1_10,DOC_B1_20 from DOC_B4 ";
            cmdText += "left join DOC_B1  on DOC_B1_10 = DOC_B4_30";
            cmdText += " where DOC_B4_50=0 " + sQuery + "";
            List<MsCodeRule> lstMsCodeRule = null;
            using (IDataReader iReader = DataAccessor.ExecuteReader(cmdText))
            {
                lstMsCodeRule = new List<MsCodeRule>();
                while (iReader.Read())
                {
                    MsCodeRule ms = new MsCodeRule();
                    ms.CodeId = iReader["DOC_B4_10"].ToString();
                    ms.CodeName = iReader["DOC_B4_20"].ToString();
                    lstMsCodeRule.Add(ms);
                }
            }

            return lstMsCodeRule;
        }
        #endregion

        #region 保存编号规则信息
        /// <summary>
        /// 保存编号规则信息
        /// </summary>
        /// <param name="ms"></param>
        /// <returns></returns>
        public List<string> DoSaveCodeRuleMes(MsCodeRule ms)
        {
            string cmdText = "";
            List<string> lstResult = new List<string>();
            List<string> lstCmdText = new List<string>();
            List<MsCodeRuleDetail> lstDetail = ms.LstDetail;
            LoginUser loginUser = (LoginUser)HttpContext.Current.Session["LogUserInfo"];
            cmdText = " update DOC_B4 set DOC_B4_40=0 where DOC_B4_30='"+ms.Primary+"'";
            lstCmdText.Add(cmdText);
            if (ms.CodeId == "")
            {
                string curYear = DateTime.Now.ToString("yyyy-MM-dd");
                ms.CodeId = DataInterface.GetMaxId("DOC_B4", "DOC_B4_10").ToString();
                cmdText = "insert into DOC_B4 values(" + ms.CodeId + ",'" + ms.CodeName.Replace("'", "''") + "'" +
                    ",'" + ms.Primary + "'," + ms.CodeDefault + ",0," +
                    "'" + loginUser.OrganId + "','" + loginUser.DeptId + "','" + loginUser.PosId + "','" + loginUser.ClassId + "','" + loginUser.UserId + "','" + curYear + "')";
                lstCmdText.Add(cmdText);
            }
            else
            {
                cmdText = "update DOC_B4 set DOC_B4_20='" + ms.CodeName.Replace("'", "''") + "',DOC_B4_40=" + ms.CodeDefault + " where DOC_B4_10=" + ms.CodeId;
                lstCmdText.Add(cmdText);
            }
            DoSaveDetail(lstDetail, ref lstCmdText, ms.CodeId);
            int iNum = DataAccessor.ExecuteNonQuery(lstCmdText);
            if (iNum > 0)
            {
                lstResult.Add(ms.CodeId);
                lstResult.Add(ms.CodeName);
                lstResult.Add("操作成功!");
            }
            else
            {
                lstResult.Add("-999");
                lstResult.Add("");
                lstResult.Add("服务器忙,请稍候再试!");
            }
            return lstResult;
        }
        private void DoSaveDetail(List<MsCodeRuleDetail> msDetail, ref List<string> lstCmdText, string codeId)
        {
            if (msDetail != null)
            {
                string cmdText = "delete DOC_B5 where DOC_B5_20=" + codeId;
                lstCmdText.Add(cmdText);
                for (int i = 0; i < msDetail.Count; i++)
                {
                    MsCodeRuleDetail msCodeDetail = (MsCodeRuleDetail)msDetail[i];
                    int sDetailId = DataInterface.GetMaxId("DOC_B5", "DOC_B5_10");
                    cmdText = "insert into DOC_B5 values(" + sDetailId + "," + codeId + ",1,'" + msCodeDetail.OneCodeNum + "','" + msCodeDetail.OneCons + "'," +
                        "'" + msCodeDetail.OneMedian + "','" + msCodeDetail.OneSeparator + "')";
                    lstCmdText.Add(cmdText);
                    cmdText = "insert into DOC_B5 values(" + (sDetailId + 1) + "," + codeId + ",2,'" + msCodeDetail.TwoCodeNum + "','" + msCodeDetail.TwoCons + "'," +
                        "'" + msCodeDetail.TwoMedian + "','" + msCodeDetail.TwoSeparator + "')";
                    lstCmdText.Add(cmdText);
                    cmdText = "insert into DOC_B5 values(" + (sDetailId + 2) + "," + codeId + ",3,'" + msCodeDetail.ThreeCodeNum + "','" + msCodeDetail.ThreeCons + "'," +
                        "'" + msCodeDetail.ThreeMedian + "','" + msCodeDetail.ThreeSeparator + "')";
                    lstCmdText.Add(cmdText);
                    cmdText = "insert into DOC_B5 values(" + (sDetailId + 3) + "," + codeId + ",4,'" + msCodeDetail.FourCodeNum + "','" + msCodeDetail.FourCons + "'," +
                        "'" + msCodeDetail.FourMedian + "','" + msCodeDetail.FourSeparator + "')";
                    lstCmdText.Add(cmdText);
                    cmdText = "insert into DOC_B5 values(" + (sDetailId + 4) + "," + codeId + ",5,'" + msCodeDetail.FiveCodeNum + "','" + msCodeDetail.FiveCons + "'," +
                        "'" + msCodeDetail.FiveMedian + "','" + msCodeDetail.FiveSeparator + "')";
                    lstCmdText.Add(cmdText);
                    break;
                }
            }
        }
        #endregion

        #region 删除规则信息
        /// <summary>
        /// 删除规则信息
        /// </summary>
        /// <param name="codeId"></param>
        /// <returns></returns>
        public List<string> DoOperateCodeRule(string codeId)
        {
            string cmdText = "update DOC_B4 set DOC_B4_50=1 where DOC_B4_10=" + codeId;
            List<string> lstResult = new List<string>();
            int iNum = DataAccessor.ExecuteNonQuery(cmdText);
            if (iNum > 0)
            {
                lstResult.Add(codeId);
                lstResult.Add("");
                lstResult.Add("操作成功!");
            }
            else
            {
                lstResult.Add("-999");
                lstResult.Add("");
                lstResult.Add("服务器忙,请稍候再试!");
            }
            return lstResult;
        }

        #endregion

        #region 获取规则名称信息
        /// <summary>
        /// 获取规则名称信息
        /// </summary>
        /// <param name="codeId"></param>
        /// <returns></returns>
        public MsCodeRule GetCodeRule(string codeId)
        {
            string cmdText = "select DOC_B4_20,DOC_B1_10,DOC_B4_40 from DOC_B4 left join DOC_B1 on DOC_B1_10=DOC_B4_30";
            cmdText += " where DOC_B4_10=" + codeId + " and DOC_B4_50=0";
            MsCodeRule ms = null;
            using (IDataReader iReader = DataAccessor.ExecuteReader(cmdText))
            {
                if (iReader.Read())
                {
                    ms = new MsCodeRule();
                    ms.CodeName = iReader["DOC_B4_20"].ToString();
                    ms.Primary = iReader["DOC_B1_10"].ToString();
                    ms.CodeDefault = iReader["DOC_B4_40"].ToString();
                }
            }
            return ms;
        }
        /// <summary>
        /// 获取规则明细信息
        /// </summary>
        /// <param name="codeId"></param>
        /// <returns></returns>
        public List<MsCodeRuleDetail> GetCodeRuleDetailMes(string codeId)
        {
            string cmdText = "select DOC_B5_10,DOC_B5_20,DOC_B5_30,DOC_B5_40,DOC_B5_50,DOC_B5_60,DOC_B5_70 from DOC_B5 ";
            cmdText += " where DOC_B5_20=" + codeId;
            List<MsCodeRuleDetail> lstDetail = null;
            using (IDataReader iReader = DataAccessor.ExecuteReader(cmdText))
            {
                lstDetail = new List<MsCodeRuleDetail>();
                while (iReader.Read())
                {
                    MsCodeRuleDetail ms = new MsCodeRuleDetail();
                    if (iReader["DOC_B5_30"].ToString() == "1")
                    {
                        ms.OneCodeNum = iReader["DOC_B5_40"].ToString();
                        ms.OneCons = iReader["DOC_B5_50"].ToString();
                        ms.OneMedian = iReader["DOC_B5_60"].ToString();
                        ms.OneSeparator = iReader["DOC_B5_70"].ToString();
                    }
                    if (iReader["DOC_B5_30"].ToString() == "2")
                    {
                        ms.TwoCodeNum = iReader["DOC_B5_40"].ToString();
                        ms.TwoCons = iReader["DOC_B5_50"].ToString();
                        ms.TwoMedian = iReader["DOC_B5_60"].ToString();
                        ms.TwoSeparator = iReader["DOC_B5_70"].ToString();
                    }
                    if (iReader["DOC_B5_30"].ToString() == "3")
                    {
                        ms.ThreeCodeNum = iReader["DOC_B5_40"].ToString();
                        ms.ThreeCons = iReader["DOC_B5_50"].ToString();
                        ms.ThreeMedian = iReader["DOC_B5_60"].ToString();
                        ms.ThreeSeparator = iReader["DOC_B5_70"].ToString();
                    }
                    if (iReader["DOC_B5_30"].ToString() == "4")
                    {
                        ms.FourCodeNum = iReader["DOC_B5_40"].ToString();
                        ms.FourCons = iReader["DOC_B5_50"].ToString();
                        ms.FourMedian = iReader["DOC_B5_60"].ToString();
                        ms.FourSeparator = iReader["DOC_B5_70"].ToString();
                    }
                    if (iReader["DOC_B5_30"].ToString() == "5")
                    {
                        ms.FiveCodeNum = iReader["DOC_B5_40"].ToString();
                        ms.FiveCons = iReader["DOC_B5_50"].ToString();
                        ms.FiveMedian = iReader["DOC_B5_60"].ToString();
                        ms.FiveSeparator = iReader["DOC_B5_70"].ToString();
                    }
                    lstDetail.Add(ms);
                }
                return lstDetail;
            }
        }

        #endregion

        #region 获取树第一层节点信息

        /// <summary>
        /// 获取树第一层节点信息

        /// </summary>
        /// <param name="sClassType"></param>
        /// <returns></returns>
        public string GetCodeRuleNodesMes(string sClassType)
        {
            string sQuery = "";
            if (sClassType != "")
                sQuery = "and DOC_B4_30='" + sClassType + "'";
            string cmdText = "select Distinct DOC_B1_10,DOC_B1_20 from doc_b1 left join DOC_B4  on DOC_B1_10 = DOC_B4_30  ";
            cmdText += " where DOC_B4_50=0 " + sQuery + "";
            StringBuilder sbXML = new StringBuilder("<ADDRS>");
            using (IDataReader iReader = DataAccessor.ExecuteReader(cmdText))
            {
                string hasChild = "0";
                while (iReader.Read())
                {
                    hasChild = (isCodeRuleChildAddr(iReader["DOC_B1_10"].ToString())) ? "1" : "0";
                    sbXML.Append("<ADDR HASCHILD=\"").Append(hasChild).Append("\">");
                    sbXML.Append("<CodeID>").Append(iReader["DOC_B1_10"].ToString()).Append("</CodeID>");
                    sbXML.Append("<CodeNAME><![CDATA[").Append(iReader["DOC_B1_20"].ToString()).Append("]]></CodeNAME>");
                    sbXML.Append("</ADDR>");
                }
            }
            sbXML.Append("</ADDRS>");
            return sbXML.ToString();
        }
        private bool isCodeRuleChildAddr(string sClassType)
        {
            bool bHas = false;
            if (string.IsNullOrEmpty(sClassType)) return bHas;

            string cmdText = "SELECT COUNT(DOC_B4_10) AS CHILDCOUNT FROM DOC_B4 WHERE DOC_B4_30= '" + sClassType + "' ";

            string childCount = "";
            using (IDataReader iReader = DataAccessor.ExecuteReader(cmdText))
            {
                if (iReader.Read() && null != iReader[0])
                {
                    childCount = iReader["CHILDCOUNT"].ToString();
                    if (!string.IsNullOrEmpty(childCount) && childCount != "0")
                        bHas = true;
                }
            }

            return bHas;
        }
        #endregion

        #region 删除文档编码规则表信息(doc_b8)
        /// <summary>
        /// 删除文档编码规则表信息(doc_b8)
        /// </summary>
        /// <param name="Number"></param>
        /// <param name="codeId"></param>
        /// <returns></returns>
        public List<string> DoOperateCodeManage(string Number, string codeId)
        {
            List<string> lstResult = new List<string>();
            string cmdText = "";
            if (codeId != "")
            {
                cmdText = "delete from doc_b8 where DOC_B8_20=" + codeId;
                lstResult.Add(codeId);
            }
            else
            {
                cmdText = "delete from doc_b8 where DOC_B8_10 in('" + Number + "')";
                lstResult.Add(Number);
            }

            int iNum = DataAccessor.ExecuteNonQuery(cmdText);
            if (iNum > 0)
            {
                return lstResult;
            }
            else
            {
                lstResult.Add("-999");
                return lstResult;
            }
        }

        #endregion

        #region 获取文档编码规则表信息

        /// <summary>
        /// 获取文档编码规则表信息

        /// </summary>
        /// <param name="sNumber"></param>
        /// <returns></returns>
        public DataSet GetCodeManage(string codeId, string sNumber)
        {
            string sQuery = "";
            string NodeNum = "";
            string welCalse = "";
            if (sNumber != "")
            {
                sQuery = " and DOC_B8_10 like '%" + sNumber.Replace("'", "''") + "%'";
            }
            if (codeId != "")
            {
                NodeNum = GetPrimaryMes(codeId);
                if (NodeNum != "")
                {
                    welCalse = " and DOC_B8_30='" + NodeNum + "'";
                }
            }
            else
            { return null; }
            string cmdText = "select DOC_B8_10 as Number,DOC_B1_20 as TypeName,DOC_B4_20 as CodeName, ";
            cmdText += "DOC_B8_40 as OrderMax,DOC_B8_50 as OrderSmall  from doc_b8";
            cmdText += " left join DOC_B4 on DOC_B4_10 = DOC_B8_20  left join DOC_B1 on DOC_B1_10 = DOC_B8_30 ";
            cmdText += "where 1=1 and DOC_B8_20=" + codeId + " " + welCalse + " " + sQuery + " order by DOC_B8_9995 desc";
            DataSet dSet = DataAccessor.ExecuteDataSet(cmdText);
            return dSet;
        }
        private string GetPrimaryMes(string codeID)
        {
            string cmdText = "select DOC_B4_30 from DOC_B4 where DOC_B4_10=" + codeID;
            string NodeNum = "";
            using (IDataReader iReader = DataAccessor.ExecuteReader(cmdText))
            {
                if (iReader.Read())
                {
                    NodeNum = iReader["DOC_B4_30"].ToString();
                }
            }
            return NodeNum;
        }
        #endregion

        #region 新增文件
        public MsDocClass InsertDoc(MsDocClass Ms)
        {
            string sSql;
            #region 获取表大顺序号
            sSql = "SELECT isnull(MAX(DOC_B3_90),0) AS MaxId from DOC_B3 LEFT JOIN DOC_B2 ON ";
            sSql += "DOC_B3_50 = DOC_B2_10 WHERE DOC_B2_90 = '" + Ms.updatetime + "'";
            using (IDataReader iReader = DataAccessor.ExecuteReader(sSql))
            {
                if (iReader.Read())
                {
                    Ms.bigOrderId = (int.Parse(iReader["MaxId"].ToString())+1).ToString();
                }
            }
            #endregion
            Ms.sysdocid = Ccit.OA.Utils.DataInterface.GetMaxId("DOC_B3", "DOC_B3_10").ToString().Trim();
            Ms.updatetime = System.DateTime.Now.ToString();
            sSql = "insert into DOC_B3(DOC_B3_10,DOC_B3_20,DOC_B3_30,DOC_B3_40,DOC_B3_50,DOC_B3_60,DOC_B3_70,";
            sSql += "DOC_B3_80,DOC_B3_9990,DOC_B3_9991,DOC_B3_9992,DOC_B3_9993,DOC_B3_9994,DOC_B3_9995,DOC_B3_90)";
            sSql += " values('" + Ms.sysdocid;
            sSql += "' ,'" + Ms.syscode;
            sSql += "' ,'" + Ms.sysdoctitle;
            sSql += "' ,'" + Ms.systype;
            sSql += "','" + Ms.sysecond;
            sSql += "' ,'" + Ms.syslength;
            sSql += "','" + Ms.sysremarks;
            sSql += "' ,'" + Ms.updatetime;
            sSql += "', '" + Ms.agencId;
            sSql += "', '" + Ms.deptId;
            sSql += "' ,'" + Ms.jobId;
            sSql += "' ,'" + Ms.levelId;
            sSql += "' ,'" + Ms.cjuserId;
            sSql += "','" + Ms.cjdatetime ;
            sSql += "','" + Ms.bigOrderId + "')";
            DataAccessor.ExecuteNonQuery(sSql);
            return Ms;
        }

        public MsDocClass GetDoc(MsDocClass Ms)
        {
            string sSql;
            sSql = "select * from DOC_B3 WHERE DOC_B3_10 = " + Ms.sysdocid;
            IDataReader iReader = DataAccessor.ExecuteReader(sSql);
            if (iReader.Read())
            {

                Ms.sysdocid = iReader["doc_b3_10"].ToString();
                Ms.sysdoctitle = iReader["doc_b3_30"].ToString();
                Ms.syscode = iReader["doc_b3_20"].ToString();
                Ms.sysecond = iReader["doc_b3_50"].ToString();
                Ms.systype = iReader["doc_b3_40"].ToString();
                Ms.sysremarks = iReader["DOC_B3_70"].ToString();
                Ms.syslength = iReader["DOC_B3_60"].ToString();
                Ms.updatetime = iReader["DOC_B3_80"].ToString();
                Ms.bigOrderId = iReader["DOC_B3_90"].ToString();
                Ms.agencId = iReader["DOC_B3_9990"].ToString();
                Ms.deptId = iReader["DOC_B3_9991"].ToString();
                Ms.jobId = iReader["DOC_B3_9992"].ToString();
                Ms.levelId = iReader["DOC_B3_9993"].ToString();
                Ms.cjuserId = iReader["DOC_B3_9994"].ToString();
                Ms.cjdatetime = iReader["DOC_B3_9995"].ToString();

            }
            return Ms;
        }

        /// <summary>
        /// 更新文件信息
        /// </summary>
        /// <param name="Ms"></param>
        /// <returns></returns>
        public int UpdateDoc(MsDocClass Ms)
        {
            string sSql;
            sSql = "update DOC_B3 set doc_b3_30='" + Ms.sysdoctitle +"', ";
            sSql += "doc_b3_20='"+ Ms.syscode +"',";
            sSql += "DOC_B3_70='"+ Ms.sysremarks +"',";
            sSql += "DOC_B3_80='" + Ms.updatetime + "' ";
            sSql += " WHERE DOC_B3_10 =" + Ms.sysdocid;
            return DataAccessor.ExecuteNonQuery(sSql);
        }

        //附件 新增
        public MsDocContentClass InsertDocContent(MsDocContentClass Ms, string presentId)
        {
            string sSql = string.Empty;
            sSql = " SELECT * FROM DOC_B7 WHERE DOC_B7_10 =" + presentId;
            IDataReader iReader = DataAccessor.ExecuteReader(sSql);
            if (iReader.Read())
            {
                sSql = "INSERT INTO DOC_B7(DOC_B7_10,DOC_B7_20,DOC_B7_30) VALUES(#sId,#sVersion,#sContent)";

                IDataParameter guid = DataAccessor.GetDataParameter("sId", DataType.Int);
                IDataParameter version = DataAccessor.GetDataParameter("sVersion", DataType.Int);
                IDataParameter content = DataAccessor.GetDataParameter("sContent", DataType.Binary);

                guid.Value = Ms.sysdocId;
                version.Value = Ms.versionId;
                content.Value = (Byte[])iReader["DOC_B7_30"];

                IDataParameter[] parameters = new IDataParameter[] { guid, version, content };
                DataAccessor.ExecuteNonQuery(sSql, parameters);
            }

            return Ms;
        }
        #endregion

        #region 返回文件夹共享标志

        /// <summary>
        /// 返回 当前人所拥有的文档权限

        /// </summary>
        /// <param name="sScope">共享范围</param>
        /// <param name="sId">文件夹ID</param>
        /// <returns>00完全共享 ； 01只读共享；05未共享给当前人；06本文件夹无任操作权限但下级文件夹有只读或完全操作的权限；</returns>
        public string GetShare(string sId)
        {
            string sWhere = "";
            string sReturn = "05";
            string sContectIdList = "";
            string sUserID, sReadIdList, sCompleteIdList;
            LoginUser loginUser = (LoginUser)HttpContext.Current.Session["LogUserInfo"];
            sWhere = " and DOC_B2_10 =" + sId;
            List<MsSecondClass> mslist = GetMsSecondItem(sWhere);
            sUserID = "," + loginUser.UserId + ",";
            if (mslist.Count != 0)
            {

                //根据共享范围与共享标志得到文件夹 只读的共享用户ID串

                sReadIdList = "," + Ccit.OA.Utils.SysInterface.GetUserIdsByRangeType(mslist[0].Viewmark, mslist[0].Viewid, loginUser.OrganId) + ",";
                //根据共享范围与共享标志得到文件夹 完全的共享用户ID串

                sCompleteIdList = "," + Ccit.OA.Utils.SysInterface.GetUserIdsByRangeType(mslist[0].Managemark, mslist[0].Manageid, loginUser.OrganId) + ",";
                if (sReadIdList.IndexOf(sUserID) > -1)
                {
                    sReturn = "01";
                }
                if (sCompleteIdList.IndexOf(sUserID) > -1)
                {
                    sReturn = "00";
                }
            }



            #region 判断文件夹下的子文件中是否存在共享

            if (sReturn == "05")
            {
                GetLowLevFolderID(sId, ref sContectIdList);
                sWhere = " and DOC_B2_40 in(" + sId + ")";
                mslist = GetMsSecondItem(sWhere);
                if (mslist.Count != 0)
                {
                    for (int i = 0; i < mslist.Count; i++)
                    {
                        //根据共享范围与共享标志得到文件夹 只读的共享用户ID串

                        sReadIdList = "," + Ccit.OA.Utils.SysInterface.GetUserIdsByRangeType(mslist[0].Viewmark, mslist[0].Viewid, loginUser.OrganId) + ",";
                        //根据共享范围与共享标志得到文件夹 完全的共享用户ID串

                        sCompleteIdList = "," + Ccit.OA.Utils.SysInterface.GetUserIdsByRangeType(mslist[0].Managemark, mslist[0].Manageid, loginUser.OrganId) + ",";
                        if (sReadIdList.IndexOf(sUserID) > -1 || sCompleteIdList.IndexOf(sUserID) > -1)
                        {
                            sReturn = "06";
                            break;
                        }
                    }
                }
            }
            #endregion
            return sReturn;
        }
        #endregion

        #region 返回文件夹下所有文件与下级文件夹信息

        public DataSet GetFilesMess(MsSecondClass Ms)
        {
            string sSql;
            sSql = "select Doc_B3_10 AS ID,DOC_B3_30 AS Title,DOC_B3_40 AS FileType,DOC_B3_60 AS Length,DOC_B3_80 AS Updatetime,ISNULL(DOC_B3_90,1) AS BigOrderId, DOC_B3_20 AS Number,";
            sSql += "(select DOC_B2_30 from DOC_B2 WHERE DOC_B2_10 = " + Ms.Sysdocid + ") AS URL ";
            sSql += "from DOC_B3 ";
            sSql += "where 1=1 and DOC_B3_50 = " + Ms.Sysdocid;
            DataSet dSet = DataAccessor.ExecuteDataSet(sSql);
            return dSet;
        }
        #endregion

        #region 根据条件返回系统文档下边 文件夹集合

        public List<MsSecondClass> GetMsSecondItem(string sWhere)
        {
            List<MsSecondClass> MsList = new List<MsSecondClass>();
            string sSql;
            sSql = "select * from DOC_B2 WHERE 1=1 AND DOC_B2_100 = '0' ";
            if (sWhere != "")
            {
                sSql += sWhere;
            }
            IDataReader iReader = DataAccessor.ExecuteReader(sSql);
            while (iReader.Read())
            {
                MsSecondClass ms = new MsSecondClass();
                ms.Sysdocid = iReader["DOC_B2_10"].ToString();
                ms.Sysnumber = iReader["DOC_B2_20"].ToString();
                ms.Systypename = iReader["DOC_B2_30"].ToString();
                ms.Superiorid = iReader["DOC_B2_40"].ToString();
                ms.Manageid = iReader["DOC_B2_60"].ToString();
                ms.Managemark = iReader["DOC_B2_50"].ToString();
                ms.Viewid = iReader["DOC_B2_80"].ToString();
                ms.Viewmark = iReader["DOC_B2_70"].ToString();
                MsList.Add(ms);

            }
            return MsList;
        }
        #endregion

        #region 获取目录下所有子节点ID
        public void GetLowLevFolderID(string higfolderid, ref string sConnectID)
        {
            string sShare = "";
            if (sConnectID == "")
            {
                sConnectID = higfolderid + ",";
            }

            string sSql;

            sSql = "Select DOC_B2_10 from DOC_B2 where DOC_B2_40 IN (" + higfolderid + ")";
            DataSet dSet = DataAccessor.ExecuteDataSet(sSql);
            if (dSet.Tables[0].Rows.Count != 0)
            {
                higfolderid = "";
                for (int i = 0; i < dSet.Tables[0].Rows.Count; i++)
                {
                    //sShare = GetShare(dSet.Tables[0].Rows[i]["DOC_B2_10"].ToString().Trim());
                    //if (sShare == "00" || sShare == "01")
                    //{
                    sConnectID += dSet.Tables[0].Rows[i]["DOC_B2_10"].ToString().Trim();
                    sConnectID += ",";

                    higfolderid += dSet.Tables[0].Rows[i]["DOC_B2_10"].ToString().Trim();
                    higfolderid += ",";
                    //}
                }
                GetLowLevFolderID(higfolderid.Trim(','), ref sConnectID);
            }
        }
        #endregion

        #region 根据一级类别编号规则 生成文档编号
        public string GetEncodRules(string sClassType, string FoldrCode)
        {
            
            string sSql,sRulesign,RulesId, sEncodRules = "",sRulesType,sEncodValue="",sMax="1";
            int sLen =0;
            sSql = "select B.*,A.DOC_B4_10 from DOC_B4 A LEFT JOIN DOC_B5 b ON A.DOC_B4_10 = B.DOC_B5_20 ";
            sSql += " WHERE A.DOC_B4_40 = '1' and A.DOC_B4_30 = '" + sClassType + "'";
            DataSet dSet = DataAccessor.ExecuteDataSet(sSql);
            
          
            if (dSet.Tables[0].Rows.Count != 0)
            {
                #region 获取 类别序号集合
                sSql = "SELECT MAX(DOC_B3_90) AS MaxId from DOC_B3 LEFT JOIN DOC_B2 ON ";
                sSql += " DOC_B3_50 = DOC_B2_10 WHERE DOC_B2_90 = '" + sClassType.Trim() +"'";
                using (IDataReader iReader = DataAccessor.ExecuteReader(sSql))
                {
                    if (iReader.Read())
                    {
                        sMax = iReader["MaxId"].ToString();//获取当前最大顺序号
                    }
                }
                #endregion
                for (int i = 0; i < dSet.Tables[0].Rows.Count; i++)
                {
                    
                    sRulesType = dSet.Tables[0].Rows[i]["DOC_B5_40"].ToString().Trim(); //获取编码规则 级的设置
                    sLen = int.Parse(dSet.Tables[0].Rows[i]["DOC_B5_60"].ToString());  //获取位数
                    sRulesign =  dSet.Tables[0].Rows[i]["DOC_B5_70"].ToString();  //获取规则符号
                    RulesId = dSet.Tables[0].Rows[i]["DOC_B5_20"].ToString().Trim(); //获取规则Id
                   
                    if (sRulesType != "")
                    {
                       
                        #region 根据级别获取 规则字符串
                        switch (sRulesType)
                        {
                            case "0": //常量
                                sEncodRules = dSet.Tables[0].Rows[i]["DOC_B5_50"].ToString().Trim();
                                break;
                            case "1": //类别编码
                                sEncodRules = FoldrCode;
                                break;
                            case "2": //年份
                                sEncodRules = System.DateTime.Now.Year.ToString();
                                break;
                            case "3"://当前年月份
                                sEncodRules = System.DateTime.Now.Year.ToString() + System.DateTime.Now.Month.ToString();
                                break;
                            case "4"://大顺序号
                                if (sMax == "0" || sMax == "")
                                {
                                    sMax = "1";
                                }
                                else {
                                    sMax = (int.Parse(sMax) + 1).ToString();
                                }
                                sEncodRules = sMax.PadLeft(sLen, '0');
                                break;
                            case "5"://小顺序号   暂时去掉小序号
                                //if (dSetOrder.Tables[0].Rows.Count != 0)
                                //{
                                //    sMax = (int.Parse(dSetOrder.Tables[0].Rows[i]["DOC_B8_50"].ToString() + 1).ToString());
                                //}
                                //sEncodRules = sMax.PadLeft(sLen, '0');
                                //sSql = "update DOC_B8 SET DOC_B8_50 =" + sMax;
                                //sSql += " where DOC_B8_30 ='" + sClassType + "' AND DOC_B8_20 =" + RulesId;
                                //DataAccessor.ExecuteNonQuery(sSql);
                                break;
                            default:
                                break;
                        }
                        #endregion
                        #region 添加分隔符
                        switch(sRulesign)
                        {
                            case "":    //无分隔符号时
                                sEncodValue += sEncodRules;
                                break;
                            case " ":    //以 空格 为分隔符时
                                sEncodValue += sEncodRules + " ";
                                break;
                            case "+":   //以 + 为分隔符时
                                sEncodValue += sEncodRules + "+";
                                break;
                            case "-":   //以 - 为分隔符时
                                sEncodValue += sEncodRules + "-";
                                break;
                            case "_":   //以 _ 为分隔符时
                                sEncodValue += sEncodRules + "_";
                                break;
                            case "[、]":    //以 [、] 为分隔符时
                                sEncodValue += "[" + sEncodRules + "]";
                                break;
                            case "{、}":    //以 {、} 为分隔符时
                                sEncodValue += "{" + sEncodRules + "}";
                                break;
                            case "(、)":    //以 (、) 为分隔符时
                                sEncodValue += "(" + sEncodRules + ")";
                                break;
                            default:
                                break;
                        }
                        #endregion
                    }
                }
            }
            return sEncodValue;
        }
        #endregion


        /// </summary>
        /// <param name="sFiesId">文件ID串 ,号分隔</param>
        /// <param name="sWork">操作类型 Code复制/Shear剪切</param>
        /// <param name="sType">文件类型串 ,号分隔</param>
        /// <param name="sObjectID">要进行 粘贴的文件夹ID</param>
        public bool Fiesmove(string sFiesId, string sWork, string sType, string sObjectID, string sClassType)
        {
            string sSql, sWhere;
            string[] sFiesIdList = sFiesId.Split(',');
            string[] sTypeList = sType.Split(',');
            int len = sFiesIdList.Length;
            if (len != 0)
            {
                MsDocClass msdocclass = new MsDocClass();
                MsSecondClass mssecond = new MsSecondClass();
                MsDocContentClass mscontent = new MsDocContentClass();

                for (int i = 0; i < len; i++)
                {

                    #region  /*文件时 */
                    
                    msdocclass.sysdocid = sFiesIdList[i].ToString();
                    msdocclass = GetDoc(msdocclass);
                    msdocclass.sysecond = sObjectID; //上级文件夹ID
                    msdocclass.updatetime = sClassType;

                    mssecond = GetSecond(sObjectID);
                    msdocclass.syscode = GetEncodRules(sClassType, mssecond.Sysnumber);
                    
                    mscontent.versionId = "1";
                    mscontent.sysdocId = sFiesIdList[i].Trim();
                    /*文件复制*/
                    if (sWork == "Code")
                    {
                        msdocclass = InsertDoc(msdocclass);
                        mscontent.sysdocId = msdocclass.sysdocid;
                        mscontent.versionId = "1";
                        InsertDocContent(mscontent, sFiesIdList[i].Trim());
                    }
                    /*文件剪切*/
                    if (sWork == "Shear")
                    {
                        sSql = "update DOC_B3 SET DOC_B3_50 = " + sObjectID + " WHERE DOC_B3_10 =" + sFiesIdList[i].Trim();
                        DataAccessor.ExecuteNonQuery(sSql);
                    }
                    #endregion
                }
                mssecond = null;
                msdocclass = null;
                mscontent = null;
            }
            return true;

        }

        #region 复制文件夹及其子文件夹与文件
        private void CodeFolder(DataView dv, string sClassType, string Superiorid)
        {
            string sSql;
            foreach (DataRowView dr in dv)
            {
                #region 新增文件夹

                MsDocClass msfile = new MsDocClass();
                #region 新增文件夹下边所有文件

                sSql = "select DOC_B3_10 FROM DOC_B3 WHERE DOC_B3_50 = " + dr["DOC_B2_10"].ToString().Trim();
                DataSet dSet = DataAccessor.ExecuteDataSet(sSql);
                if (dSet.Tables[0].Rows.Count != 0)
                {
                    for (int i = 0; i < dSet.Tables[0].Rows.Count; i++)
                    {

                        msfile.sysdocid = dSet.Tables[0].Rows[i]["DOC_B3_10"].ToString().Trim();
                        msfile = GetDoc(msfile);
                        msfile.sysecond = Superiorid;    //所属文件夹
                        msfile = InsertDoc(msfile);
                        MsDocContentClass msversion = new MsDocContentClass();
                        msversion.sysdocId = msfile.sysdocid;
                        msversion.versionId = "1";
                        InsertDocContent(msversion, dSet.Tables[0].Rows[i]["DOC_B3_10"].ToString().Trim());
                        msfile = null;
                    }
                }
                #endregion
                #endregion
                sSql = "select * from DOC_B2 WHERE DOC_B2_40 = " + dr["DOC_B2_10"] + " AND DOC_B2_90 ='" + sClassType + "'";
                dSet = DataAccessor.ExecuteDataSet(sSql);
                if (dSet.Tables[0].Rows.Count != 0)
                {
                    DataView dvSun = new DataView(dSet.Tables[0]);
                    CodeFolder(dvSun, sClassType, dr["DOC_B2_10"].ToString().Trim());
                }
            }
        }

        #region  返回二级文件夹 信息
        public MsSecondClass GetSecond(string sysdocId)
        {
            MsSecondClass Ms = new MsSecondClass();
            string sSql;
            sSql = "SELECT * FROM DOC_B2 WHERE DOC_B2_10 =" + sysdocId;
            using (IDataReader iReader = DataAccessor.ExecuteReader(sSql))
            {
                if (iReader.Read())
                {
                    Ms.Sysdocid = iReader["DOC_B2_10"].ToString();
                    Ms.Sysnumber = iReader["DOC_B2_20"].ToString();
                    Ms.Systypename = iReader["DOC_B2_30"].ToString();
                    Ms.Superiorid = iReader["DOC_B2_40"].ToString();
                    Ms.Managemark = iReader["DOC_B2_50"].ToString();
                    Ms.Manageid = iReader["DOC_B2_60"].ToString();
                    Ms.Viewmark = iReader["DOC_B2_70"].ToString();
                    Ms.Primary = iReader["DOC_B2_90"].ToString();
                    Ms.State = iReader["DOC_B2_100"].ToString();
                    Ms.Url = iReader["DOC_B2_110"].ToString();
                    Ms.agencId = iReader["DOC_B2_9990"].ToString();
                    Ms.deptId = iReader["DOC_B2_9991"].ToString();
                    Ms.jobId = iReader["DOC_B2_9992"].ToString();
                    Ms.levelId = iReader["DOC_B2_9993"].ToString();
                    Ms.cjuserId = iReader["DOC_B2_9994"].ToString();
                    Ms.cjdatetime = iReader["DOC_B2_9995"].ToString();

                }
            }

            return Ms;
        }
        #endregion
        #endregion

        #region 删除文件
        public bool DelSysDoc(MsDocClass Ms)
        {
            bool bFlag = false;
            string sSql = string.Empty;
            string sFoldID = "";
            /*单独删除一行记录*/
            string[] IdList = Ms.sysdocid.Split(',');
            for (int i = 0; i < IdList.Length; i++)
            {
                if (IdList[i] != "")
                {
                    sSql = "Delete DOC_B3 WHERE DOC_B3_10 = " + IdList[i];
                    if (DataAccessor.ExecuteNonQuery(sSql) > -1)
                    {
                        sSql = "Delete DOC_B7 WHERE DOC_B7_10 = " + IdList[i];
                        DataAccessor.ExecuteNonQuery(sSql);
                    }
                }
            }
            return true;
        }
        #endregion

        #region 文件查询条件
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sQueryFies">文件查询条件</param>
        /// <param name="sQueryFolder">文件夹查询条件</param>
        /// <returns></returns>
        public DataSet GetFilesSearch(string sQuery)
        {
            string  sShare,sContent = "";
            LoginUser loginUser = (LoginUser)HttpContext.Current.Session["LogUserInfo"];
            XmlDocument oDom = new XmlDocument();
            oDom.LoadXml(sQuery);
            string TitleName = oDom.GetElementsByTagName("TitleName")[0].InnerText.Trim();
            string FolderId = oDom.GetElementsByTagName("FolderId")[0].InnerText.Trim();
            string StateTime = oDom.GetElementsByTagName("StateTime")[0].InnerText.Trim();
            string EndState = oDom.GetElementsByTagName("EndState")[0].InnerText.Trim();
            string BigSize = oDom.GetElementsByTagName("BigSize")[0].InnerText.Trim();
            string SmallSize = oDom.GetElementsByTagName("SmallSize")[0].InnerText.Trim();
            string ClassType = oDom.GetElementsByTagName("ClassType")[0].InnerText.Trim();
            string IncNodeFolder = oDom.GetElementsByTagName("IncNodeFolder")[0].InnerText.Trim();

            sShare = FolderId.Split('_')[1].ToString();
            FolderId = FolderId.Split('_')[0].ToString();
            DataSet dSet = new DataSet();
            if (sShare == "01" || sShare == "00" || FolderId == "0")
            {
                string sSql;
                sSql = "select Doc_B3_10 AS ID,DOC_B3_30 AS Title,DOC_B3_20 AS Number,DOC_B3_40 AS FileType,DOC_B3_60 AS Length,DOC_B3_80 AS Updatetime,cast((select DOC_B2_30 from DOC_B2 where DOC_B2_10 = DOC_B3_50) as varchar(500)) AS URL ";
                sSql += " from Doc_B3 left join DOC_B2 on DOC_B3_50 = DOC_B2_10  ";
                sSql += " where 1=1 and DOC_B2_90 ='" + ClassType + "' ";

                #region 所属文件夹ID
                if (FolderId != "0")
                {
                    if (IncNodeFolder == "00") {
                        GetSearhLowLevFolderID(FolderId, ref sContent, sShare);
                        sSql += " and DOC_B3_50 in(" + sContent.Trim(',') + ") ";
                    }
                    else {
                        sSql += " and DOC_B3_50 =" + FolderId;
                    }
                    
                }
                #endregion
                #region 标题
                if (TitleName != "")
                {
                    sSql += " and (DOC_B3_30 like '%" + TitleName + "%'  or DOC_B3_20 LIKE '%" + TitleName + "%') ";
                }
                #endregion
                #region 更新时间
                if (StateTime != "" && EndState != "")
                {
                    sSql += " and (DOC_B3_80 BETWEEN '" + StateTime + "' and '" + EndState + "')";
                }
                #endregion
                #region 大小
                if (BigSize != "" && SmallSize != "")
                {
                    sSql += " AND (DOC_B3_60 BETWEEN '" + SmallSize + "' AND '" + BigSize + "')";
                }
                else if (BigSize != "" && SmallSize == "")
                {
                    sSql += " AND (DOC_B3_60 <= " + BigSize + ")";
                }
                else if (SmallSize != "" && BigSize == "")
                {
                    sSql += " AND (DOC_B3_60 >= " + SmallSize + ")";
                }

                #endregion
                sSql += " order by DOC_B3.DOC_B3_80 ";

                dSet = DataAccessor.ExecuteDataSet(sSql);
            }
            return dSet;
        }

        /// <summary>
        /// 浏览时 根据权限 返回有可读或完全操作的文件夹
        /// </summary>
        /// <param name="higfolderid"></param>
        /// <param name="sConnectID"></param>
        public void GetSearhLowLevFolderID(string higfolderid, ref string sConnectID, string sParaShare)
        {
            string sShare = "";
            if (sParaShare != "06" && sParaShare != "05")
            {
                if (sConnectID == "")
                {
                    sConnectID = higfolderid + ",";
                }
            }


            string sSql;

            sSql = "Select DOC_B2_10 from DOC_B2 where DOC_B2_40 IN (" + higfolderid + ")";
            DataSet dSet = DataAccessor.ExecuteDataSet(sSql);
            if (dSet.Tables[0].Rows.Count != 0)
            {
                higfolderid = "";
                for (int i = 0; i < dSet.Tables[0].Rows.Count; i++)
                {
                    higfolderid += dSet.Tables[0].Rows[i]["DOC_B2_10"].ToString().Trim();
                    higfolderid += ",";
                    //上级节点为只读或完全操作时  子目录自动继承上级文件操作权限

                    if (sParaShare == "00" || sParaShare == "01")
                    {
                        sConnectID += dSet.Tables[0].Rows[i]["DOC_B2_10"].ToString().Trim();
                        sConnectID += ",";

                        GetSearhLowLevFolderID(higfolderid.Trim(','), ref sConnectID, sParaShare);
                    }
                    //上级文件夹 无任何操作权限时候

                    else if (sParaShare == "06")
                    {
                        #region 判断子目录及子目录的下级是否有操作权限

                        sShare = GetShare(dSet.Tables[0].Rows[i]["DOC_B2_10"].ToString().Trim());
                        if (sShare == "00" || sShare == "01")
                        {
                            sConnectID += dSet.Tables[0].Rows[i]["DOC_B2_10"].ToString().Trim();
                            sConnectID += ",";

                            GetSearhLowLevFolderID(higfolderid.Trim(','), ref sConnectID, sShare);
                        }
                        else if (sShare == "06")
                        {
                            GetSearhLowLevFolderID(higfolderid.Trim(','), ref sConnectID, sShare);
                        }
                        #endregion
                    }
                }

            }
        }
        #endregion


        #region
        public string GetCodeRuleMes(string sClassType)
        {
            string sQuery = "";
            //if (sNodeTab == "1")
            //{
            //    iNum = "DOC_B2_20";
            //}
            if (sClassType != "")
                sQuery = "and DOC_B1_10='" + sClassType + "'";
            string cmdText = "select DOC_B1_10,DOC_B1_20 from doc_b1 ";
            cmdText += " where 1=1 " + sQuery + "";
            StringBuilder sbXML = new StringBuilder("<ADDRS>");
            using (IDataReader iReader = DataAccessor.ExecuteReader(cmdText))
            {
                string hasChild = "0";
                while (iReader.Read())
                {
                    hasChild = (isCodeRuleChildAddr(iReader["DOC_B1_10"].ToString())) ? "1" : "0";
                    sbXML.Append("<ADDR HASCHILD=\"").Append(hasChild).Append("\">");
                    sbXML.Append("<CodeID>").Append(iReader["DOC_B1_10"].ToString()).Append("</CodeID>");
                    sbXML.Append("<CodeNAME><![CDATA[").Append(iReader["DOC_B1_20"].ToString()).Append("]]></CodeNAME>");
                    sbXML.Append("</ADDR>");
                }
            }
            sbXML.Append("</ADDRS>");
            return sbXML.ToString();
        }
        #endregion
    }

}
